package cn.iocoder.yudao.module.crm.service.customerbill;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.crm.controller.admin.customerbill.vo.CustomerBillExportVO;
import cn.iocoder.yudao.module.crm.controller.admin.customerbill.vo.CustomerBillPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.customerbill.vo.CustomerBillRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.customerbill.vo.CustomerBillSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customerbill.CustomerBillDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.pricetemplate.PriceTemplateDO;
import cn.iocoder.yudao.module.crm.dal.mysql.customer.CustomerMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.customerbill.CustomerBillMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.pricetemplate.PriceTemplateMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 客户账单 Service 实现类
 *
 * @author nail.zhang
 */
@Service
@Validated
public class CustomerBillServiceImpl implements CustomerBillService {

    @Resource
    private CustomerBillMapper customerBillMapper;

    @Resource
    private CustomerMapper  customerMapper;

    @Resource
    private PriceTemplateMapper priceTemplateMapper;

    @Override
    public Long createCustomerBill(CustomerBillSaveReqVO createReqVO) {
        // 插入
        CustomerBillDO customerBill = BeanUtils.toBean(createReqVO, CustomerBillDO.class);

        CustomerDO customerDO = customerMapper.selectById(createReqVO.getCustomerId());

        PriceTemplateDO priceTemplate = priceTemplateMapper.selectById(createReqVO.getPriceId());

        customerBill.setCompanyName(priceTemplate.getCompanyName());
        BigDecimal containerCount = BigDecimal.valueOf(createReqVO.getContainerCount());
        customerBill.setServiceFee(createReqVO.getServiceFeeUnit().multiply(containerCount));
        customerBill.setDockFee(createReqVO.getDockFeeUnit().multiply(containerCount));
        customerBill.setShipFee(createReqVO.getShipFeeUnit().multiply(containerCount));

        customerBill.setTotalFee(customerBill.getDockFee().add(customerBill.getShipFee()).add(customerBill.getServiceFee()));

        customerBillMapper.insert(customerBill);
        // 返回
        return customerBill.getId();
    }

    @Override
    public void updateCustomerBill(CustomerBillSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerBillExists(updateReqVO.getId());
        // 更新
        CustomerBillDO customerBill = BeanUtils.toBean(updateReqVO, CustomerBillDO.class);

        CustomerDO customerDO = customerMapper.selectById(updateReqVO.getCustomerId());

        PriceTemplateDO priceTemplate = priceTemplateMapper.selectById(updateReqVO.getPriceId());

        customerBill.setCompanyName(priceTemplate.getCompanyName());
        BigDecimal containerCount = BigDecimal.valueOf(updateReqVO.getContainerCount());
        customerBill.setServiceFee(updateReqVO.getServiceFeeUnit().multiply(containerCount));
        customerBill.setDockFee(updateReqVO.getDockFeeUnit().multiply(containerCount));
        customerBill.setShipFee(updateReqVO.getShipFeeUnit().multiply(containerCount));

        customerBill.setTotalFee(customerBill.getDockFee().add(customerBill.getShipFee()).add(customerBill.getServiceFee()));

        customerBillMapper.updateById(customerBill);
    }

    @Override
    public void deleteCustomerBill(Long id) {
        // 校验存在
        validateCustomerBillExists(id);
        // 删除
        customerBillMapper.deleteById(id);
    }

    private void validateCustomerBillExists(Long id) {
        if (customerBillMapper.selectById(id) == null) {
            //throw exception(CUSTOMER_BILL_NOT_EXISTS);
        }
    }

    @Override
    public CustomerBillDO getCustomerBill(Long id) {
        return customerBillMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerBillRespVO> getCustomerBillPage(CustomerBillPageReqVO pageReqVO) {

        Page<CustomerBillRespVO> mpPage = MyBatisUtils.buildPage(pageReqVO);
        mpPage = customerBillMapper.selectBillPageList(mpPage, pageReqVO);
        PageResult<CustomerBillRespVO> pageResult = new PageResult(mpPage.getRecords(), mpPage.getTotal());

        return pageResult;
        //return customerBillMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CustomerBillExportVO> getCustomerBillList(CustomerBillPageReqVO pageReqVO) {

        List<CustomerBillExportVO> list = customerBillMapper.selectBillList(pageReqVO);
        int index=1;
        for (CustomerBillExportVO billRespVO : list) {
            billRespVO.setIndexNo(index+"");
            index++;
        }
        return list;
    }

}
